// components/thumbs/index.js

let leftHeight = 0, rightHeight = 0; //分别定义左右两边的高度
let query=undefined;

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    baseimgaeUrl:{
      type:String,
      value:""
    },
    ImageData:{
      type:[],
      value:[],
      observer: function (newVal, oldVal) {

        this._ImageDatapropertyChange(newVal, oldVal);

      } // 属性被改变时执行的函数（可选），也可以写成在methods段中定义的方法名字符串, 如：'_propertyChange'
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    list:[],
    leftList:[],
    rightList:[],
    urllist:[]
    
  },

  /**
   * 组件的方法列表
   */
  methods: {

    //追加数据
     refreshImage:function(value){
 
      console.log('refreshImage');
      console.log(value);
      this.properties.ImageData = value;
      this.setData({
        list:value,
        leftList:[], 
        rightList:[],
        urllist:[],
      })
      this.isLeft();

     },
        //属性变化时触发
       _ImageDatapropertyChange:function(newVal, oldVal){
        console.log('newVal-thrumbs');
        console.log(newVal);
        this.setData({
          list:newVal,
       
        })
        this.isLeft();
       },
        //下拉刷新架追加
        _pulldownRefresh:function(e){
            
        },
        async isLeft() {
          
          const { list, leftList, rightList ,urllist} = this.data;
          console.log(this.data.list);
          
          query = this.createSelectorQuery();
          for (const item of list) {
            urllist.push( item.pic);
           leftHeight <= rightHeight ? leftList.push(item) : rightList.push(item); //判断两边高度，来觉得添加到那边
           await this.getBoxHeight(leftList, rightList);
          }
         },
         getBoxHeight(leftList, rightList) { //获取左右两边高度
         
          return new Promise((resolve, reject) => {
           this.setData({ leftList, rightList }, () => {
            query.select('#left').boundingClientRect();
            query.select('#right').boundingClientRect();
            query.exec((res) => {
              if(res[0]!=undefined){
             leftHeight = res[0].height; //获取左边列表的高度
              }
              if(res[1]!=undefined){
              rightHeight = res[1].height; //获取右边列表的高度
              }
             resolve();
            });
           });
          })
         }
         ,
         _imagepreview:function(event){
           
          var src = event.currentTarget.dataset.src;//获取data-src
          var imgList =this.data.urllist;//获取data-list
          //图片预览
          wx.previewImage({
          current: src, // 当前显示图片的http链接
          urls: imgList // 需要预览的图片http链接列表
          })
          }
         
        

  }
})
